package utils;

import javafx.scene.control.TextField;

/**
 * Extends {@link TextField} to allow only digits.
 *
 * @author Ricardo Cruz {@literal <ricardo.pdm.cruz@gmail.com>}
 * @since  2014-11-12
 */
public class NumericField extends TextField
{
	public NumericField(double value) {
		super();
		if(value%1 == 0)
			setText(String.valueOf((long)value));
		else
			setText(String.valueOf(value));
	}

	public double getValue() {
		return Double.valueOf(getText());
	}

	public void replaceText(int start, int end, String text) {
		if (!text.matches("[a-z]"))
			super.replaceText(start, end, text);
	}

	public void replaceSelection(String text) {
		if (!text.matches("[a-z]"))
			super.replaceSelection(text);
	}
}
